package com.mall.biz.pay;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.mall.client.item.MallItemService;
import com.mall.client.mallGroupBuy.MallGroupbBuyService;
import com.mall.client.mallGroupBuy.MallTuanCodeService;
import com.mall.client.order.MallItemOrderService;
import com.mall.client.trade.TradeService;
import com.mall.client.user.UserExtendedService;
import com.mall.client.user.UserService;
import com.mall.common.Consts.tradeOrderType;
import com.mall.pojo.MallGroupBuy;
import com.mall.pojo.MallItem;
import com.mall.pojo.MallItemOrder;
import com.mall.pojo.MallTuanCode;
import com.mall.pojo.Trade;
import com.mall.pojo.User;
import com.mall.pojo.UserExtended;
import com.mall.vo.item.MallItemVO;

@Service
public class PayBizImpl {
	
	private static final Logger log = LoggerFactory.getLogger(PayBizImpl.class);
	@Autowired
	private TradeService tradeService;

	@Autowired
	private MallTuanCodeService mallTuanCodeService;
	
	@Autowired
	private MallGroupbBuyService mallGroupbBuyService;
	@Autowired
	private MallItemOrderService mallItemOrderService;
	@Autowired
	private MallItemService mallItemService;
	@Autowired
	private UserExtendedService userExtendedService;
	@Autowired
	private UserService userService;
	
	
	public void paySuccessCallback(Long tradeId) {
		log.info("tradeId = " + tradeId);
		Trade trade = this.tradeService.getTradeByOrderId(tradeId);
		switch (trade.getType()) {
		case tradeOrderType.TYPE_QIYE:
			//payNormalSuccessCallback(trade);
			break;
		case tradeOrderType.TYPE_YOUHUI:
			payMaidanSuccessCallback(trade);
			break;
		case tradeOrderType.TYPE_MALLGROUPBUY:
			payMallGroupBuySuccessCallback(trade);
			break;
		case tradeOrderType.TYPE_ZHIHUI:case  tradeOrderType.WEISHANG:case  tradeOrderType.YUEZHIFU:
			payZhihuiMallSuccessCallback(trade);
			break;
		case tradeOrderType.TYPE_MINSU:
			payMinsuSuccessCallback(trade);
			break;
		case tradeOrderType.CHONGZHI:
			payRechargeSuccessCallback(trade);
			break;
		
		
		}
		
	}

	private void payRechargeSuccessCallback(Trade trade) {
		User user = this.userService.getUserByUserId(trade.getBuyUserId());
		UserExtended userExtended = this.userExtendedService.getUserExtendByUserId(trade.getBuyUserId());
		if(trade.getBuyItemType() != null && trade.getBuyItemType() == 1){
			userExtended.setBond(userExtended.getBond()+trade.getPrice());
		}
		if(trade.getBuyItemType() != null && trade.getBuyItemType() == 2){
			userExtended.setBalance(userExtended.getBalance()+trade.getPrice());
		}
		userExtended.setGmtModify(new Date());
		this.userExtendedService.updateExtendedByPrimaryKey(userExtended);
		
		user.setLevel(Short.parseShort("-"+trade.getAttribute()));
		user.setGmtModify(new Date());
		this.userService.modifytUserByPrimaryKey(user);
	}

	private void payMinsuSuccessCallback(Trade trade) {
		// TODO Auto-generated method stub
		
	}

	private void payZhihuiMallSuccessCallback(Trade trade) {
		Long mallItemOrderId = trade.getParentId();
		if(mallItemOrderId == null){
			return;
		}
		MallItemOrder mallItemOrder = this.mallItemOrderService.getMallItemOrderById(mallItemOrderId);
		if(mallItemOrder == null){
			return;
		}
		mallItemOrder.setGmtModify(new Date());
		mallItemOrder.setStatus(2);//订单支付完成回调
		
		this.mallItemOrderService.modifyMallItemOrder(mallItemOrder);
		List<MallItemVO> userZhiHuiTradeDetailVOList = null;
		String itemListJson = mallItemOrder.getBuyItemsJson();
		if (itemListJson != null) {
			userZhiHuiTradeDetailVOList = JSONArray.parseArray(itemListJson, MallItemVO.class);
		}
		MallItemVO mallItemVO = null;
		if(userZhiHuiTradeDetailVOList != null){
			for (int i = 0; i < userZhiHuiTradeDetailVOList.size(); i++) {
				mallItemVO = userZhiHuiTradeDetailVOList.get(i);
				Long mallItemId = mallItemVO.getMallItemId();
				if(mallItemId == null){
					return;
				}
				MallItem mallItem = this.mallItemService.getMallItemById(mallItemId);
				if (mallItem != null) {
					mallItem.setSaleNo(mallItem.getSaleNo()+mallItemVO.getNo());
					mallItem.setGmtModify(new Date());
					if (mallItem.getStock()>0 && mallItem.getStock()>=mallItemVO.getNo()) {
						mallItem.setStock(mallItem.getStock()-mallItemVO.getNo());	
					}
					this.mallItemService.modifyMallItemByPrimarykey(mallItem);
				}
			}
		}
		
	}

	private void payMallGroupBuySuccessCallback(Trade trade) {
		MallGroupBuy mallGroupBuy = this.mallGroupbBuyService.getMallGroupBuyById(trade.getParentId());
		mallGroupBuy.setGmtModify(new Date());
		mallGroupBuy.setSaleNo(mallGroupBuy.getSaleNo()+trade.getBuyCount());//修改销售数量
		this.mallGroupbBuyService.modifyMallGroupBuyBuyByPrimaryKey(mallGroupBuy);
		
	}

	private void payMaidanSuccessCallback(Trade trade) {
		// TODO Auto-generated method stub
		
	}
	
	
	
	/**
	 * 团购生成的二维码传递给成功页面
	 * @param trade
	 * @return
	 */
	public String TuanCodeAll(Trade trade){
		List<MallTuanCode> mallTuanCodeList = this.mallTuanCodeService.getMallTuanCodeListByTradeAndUserId(trade.getBuyUserId(), trade.getId());
		String tuancode = null;
		MallTuanCode mallTuanCode = null;
		for (int i = 0; i < mallTuanCodeList.size(); i++) {
			mallTuanCode = mallTuanCodeList.get(i);
			tuancode=mallTuanCode.getCode()+","+tuancode;
		}
		if(mallTuanCodeList.size()>0){
			tuancode = tuancode.substring(0,tuancode.length()-5);
		}
		return tuancode;
		
	}

}
